package com.day240701.controller;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "TestListenerServlet", value = "/testListener")
public class TestListenerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("testListener Servlet");
        PrintWriter out = response.getWriter();
        System.out.println("首次获得 session 对象");
        HttpSession session = request.getSession();
        out.println("hello");
        System.out.println("添加了 session 的 msg 属性");
        session.setAttribute("msg", "添加了 session 的 msg");

        System.out.println("修改了 session 的 msg（替换）");
        session.setAttribute("msg", "添加了 session 新的 msg");

        // 手动销毁 session 的 msg 属性
        System.out.println("手动销毁（移除） session中的msg属性");
        session.removeAttribute("msg");

        // 销毁 session
        System.out.println("session 对象即将被销毁");
        session.invalidate();
    }
}
